<!--
 * @Author: 徐建辰
 * @Date: 2022-06-06 10:00:56
 * @LastEditTime: 2022-06-06 10:05:52
 * @LastEditors: 徐建辰
 * @Description: svg组件
-->
<template>
  <div 
    v-if="isExternalIcon" 
    :style="styleExternalIcon" 
    class="svg-external-icon svg-icon"
    :class="className"
  />
  <svg 
    v-else 
    class="svg-icon" 
    :class="className" 
    aria-hidden="true"
    :style="{color: theme === 'default' ? '#333' : '#fff'}"
  >
    <use :xlink:href="iconName" />
  </svg> 
</template>

<script setup>
import {computed} from 'vue'
import {isExternal} from './util'
import useConfigStore from '@/store/config'

// eslint-disable-next-line no-undef
const props = defineProps({
  icon: {
    type: String,
    required: true
  },
  // 图标类名
  className: {
    type: String,
    default: ''
  },
  styles: {
    type: Object,
    default: () => {}
  }
})

const configStore = useConfigStore()

const theme = computed(() => configStore.theme)

// 是否是外部图标
const isExternalIcon = computed(() => isExternal(props.icon))
const styleExternalIcon = computed(() => ({
  mask: `url(${props.icon}) no-repeat 50% 50%`,
  '-webkit-mask': `url(${props.icon}) no-repeat 50% 50%`,
  ...props.styles
}))
const iconName = computed(() => `#icon-${props.icon}`)
</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
.svg-external-icon {
  background-color: currentColor;
  mask-size: cover;
  display: inline-block;
}
</style>